package com.alex.aop.configByAnnotation;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

// 把这个类声明为一个切面
@Order(10)
@Aspect
@Component
public class LoggingAspect {

    // 声明切入点表达式(可选)
    @Pointcut("execution(* com.alex.aop.configByAnnotation.impl.*.*(..))")
    public void declaredJoinPointExpression() {}

    // 声明该方法为前置通知: 在匹配的所有目标方法开始之前执行
    @Before("declaredJoinPointExpression()")
    public void beforeMethod(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());

        System.out.println("this method " + methodName + " begins with " + args);
    }

    // 声明该方法为后置通知: 在目标方法执行后执行的通知(无论是否发生异常) 或 返回通知
    // 后置通知并不能访问到执行的返回结果, 这需要返回通知来完成
    @After("execution(* com.alex.aop.configByAnnotation.impl.*.*(int , int))")
    public void afterMethod(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();

        System.out.println("this method "  + methodName + " ends a");
    }
    @After("execution(* com.alex.aop.configByAnnotation.impl.*.*(int , int))")
    public void afterMethod2(JoinPoint joinPoint) {
        String methodName = joinPoint.getSignature().getName();

        System.out.println("this method "  + methodName + " ends b");
    }

    // 声明一个返回通知: 在方法正常执行完毕后返回的通知(未抛出异常)
    @AfterReturning(value = "execution(* com.alex.aop.configByAnnotation.impl.*.*(int , int))", returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result) {
        String methodName = joinPoint.getSignature().getName();

        System.out.println("this method "  + methodName + " returns with " + result);
    }

    // 声明一个异常通知: 在方法正常执行异常后的通知
    // 可以访问到异常对象(所有的切面可以理解为在运行时切入对应位置横切点的一段代码, 因而系统设计时允许访问到相应上下文的对象资源)
    // 而且可以为入参指定具体的异常类型,仅在匹配对应的异常类型及子类异常时才执行通知
    @AfterThrowing(value = "execution(* com.alex.aop.configByAnnotation.impl.*.*(int , int))", throwing = "throwing")
    public void afterThrowing(JoinPoint joinPoint, Exception throwing) {
        String methodName = joinPoint.getSignature().getName();

        System.out.println("this method "  + methodName + " throws with " + throwing);
    }

    // 声明一个环绕通知:

    /**
     *
     * @param pjd
     */
    @Around(value = "execution(* com.alex.aop.configByAnnotation.impl.*.*(int , int))")
    public Object around(ProceedingJoinPoint pjd) {
        Object[] args = pjd.getArgs();
        String methodName = pjd.getSignature().getName();
        System.out.println("this method "   + " around a");
        Object result = null;
        try {
            System.out.println("before");
            result = pjd.proceed();
            System.out.println("after");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("afterThrowing");
            throw new RuntimeException(throwable);
        }
        System.out.println("afterReturning");
        return result;
    }
}
